<?php
class DvutsvsController extends AppController {

	var $name = 'Dvutsvs';
    var $uses = array('Dvutsv','Phanloaisv','Sinhvien','Z5plsv','Z1dvutsv','Chidoansv','Z1dvutcb');
    /**
     * function index()
     * Loc ra danh sach doan vien xuat sac tu ds phan loai doan vien
     * Bi thu check lai ds doan vien uu tu, dien so nguoi dong y va so nguoi tham gia bau vao roi luu vao csdl
     * */
    
	function index($cd=null) {
        $duplicate="";
        $error="";
        $daduyet="";
        $this->Dvutsv->recursive = -1;
        $hocky=$this->getHocKyHt();
        $cd=$this->Session->read('chidoanht_id');
        //lay danh sach doan vien uu tu tu bang phan loai dv
        $dvutsvs=$this->Z5plsv->find('all', array(
            'conditions'=> array('xeploai_id'=>1,'hocky_id'=>$hocky['Hocky']['id'],'chidoansv_id'=>$cd)));
        //pr($dvutsvs);
        $tongso=$this->Z5plsv->find('count', array(
            'conditions'=> array('hocky_id'=>$hocky['Hocky']['id'],'chidoansv_id'=>$cd)));
        $this->set(compact('dvutsvs', 'tongso'));
        $data=$this->data;
        //pr($data);
        if (!empty($data)) {
            $songuoithamgiabau=trim($data['Dvutsv']['songuoithamgiabau']);
            if($songuoithamgiabau==''||$songuoithamgiabau<=0){
                $this->Session->setFlash(__('Số người tham gia bầu phải lớn hơn 0', true));
            }elseif($songuoithamgiabau>$tongso){
                $this->Session->setFlash(__('Số người tham gia bầu phải nhỏ hơn tổng số đoàn viên của chi đoàn', true));
            }else{
                for($j=0;$j<(count($data['Dvutsv'])-1)/4;$j++){ //tru di 1 vi bo di 2 phan tu trong mang, chia 3 la so luong form
                    $dvutsv=null;
                    if(trim($data['Dvutsv']['songuoidongy'.$j])!='' && $data['Dvutsv']['songuoidongy'.$j]<=$songuoithamgiabau&&$data['Dvutsv']['songuoidongy'.$j]>0){
                        $dvutsv[]=array('sinhvien_id'=>$data['Dvutsv']['sinhvien_id'.$j],
                                    'hocky_id'=>$hocky['Hocky']['id'],
                                    'songuoidongy'=>$data['Dvutsv']['songuoidongy'.$j],
                                    'songuoithamgiabau'=>$songuoithamgiabau,
                                    'ghichu'=>$data['Dvutsv']['ghichu'.$j]
                        );
                        $dvut=$this->Dvutsv->find('first',array('conditions'=>array('sinhvien_id'=>$data['Dvutsv']['sinhvien_id'.$j],'hocky_id'=>$hocky['Hocky']['id']),'recursive'=>-1));                               
            			if($dvut==null){
                            $this->Dvutsv->create();
                            $this->Dvutsv->saveAll($dvutsv);
                            $this->Dvutsv->id=false;
            			}else if($dvut['Dvutsv']['duyet']==1){
                    			 $daduyet=$daduyet.'MSSV: '.$data['Dvutsv']['mssv'.$j].' đã được duyệt là đoàn viên ưu tú!!<br/>';
                                 $this->set('daduyet',$daduyet);}
                            else{
            			     $duplicate=$duplicate.'MSSV: '.$data['Dvutsv']['mssv'.$j].' đã được bình xét đoàn viên ưu tú rồi!!<br/>';
                             $this->set('duplicate',$duplicate);
            			     }
                    }else if($data['Dvutsv']['songuoidongy'.$j]>$songuoithamgiabau){
                        $error=$error.'MSSV: '.$data['Dvutsv']['mssv'.$j].' có số người đồng ý lớn hơn số người tham gia bầu?<br/>';
                        $this->set('error',$error);
                    } 
        	   }
            }
        }
	}
    /**
     * function viewdvut()
     * Xem danh sach doan vien uu tu cua chi doan hien tai, trong hoc ky hien tai sau khi da binh xet dvut 
     * */
    function viewdvut(){
        $hk=$this->getHocKyHt();
        $cd=$this->Session->read('chidoanht_id');
        $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cd)));
       // pr($dvutsvs);
        $this->set('dvutsvs',$dvutsvs);
    }
    /** exp mau4
     * 
     * /
     * */
     
    function reviewdvut(){
        $hk=$this->getHocKyHt();
        //$khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $dvutsvs=$this->Z1dvutsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id']),'recursive'=>0));
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $this->set(compact('dvutsvs','dvutcbs'));
        //pr($dvutcbs);
    }
    function ketquadvut(){
        $this->layout = 'excel';
        $hk=$this->getHocKyHt();
        //$khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $dvutsvs=$this->Z1dvutsv->find('all',array('conditions'=>array('hocky_id'=>$hk['Hocky']['id']),'recursive'=>0));
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $this->set(compact('dvutsvs','dvutcbs'));
        //pr($dvutcbs);
    }
    /**
     * function danhsachcd()
     * Liet ke danh sach cac chi doan de duyet 
     * */
    function danhsachcd(){
        $chidoansvs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id <>'=>null)));
        $this->set(compact('chidoansvs'));
        $model='Chidoansv';
        if(!empty($this->data)){
            //pr($this->data);exit;
            if(trim($this->data['Dvutsv']['data'])!=''){
                $results=$this->$model->find('all',array(
                                            'conditions'=>array(
                                                            'or'=>array(
                                                                $model.'.machidoansv Like '=>'%'.$this->data['Dvutsv']['data']."%",
                                                                $model.'.tenchidoan Like '=>'%'.$this->data['Dvutsv']['data'].'%'
                ))));
                if(!empty($results)){
                    $this->set('results', $results);    
                }else {$this->set('check', "No results!");}
                
            }
        }
    }
    /**
     * function duyetdvut($cdid=null)
     * The hien form duyet doan vien uu tu, cdid la id cua chi doan dang duyet 
     * */
    function duyetdvut($cdid=null){
       $hk=$this->getHocKyHt();
       $dvutsvs=$this->Z1dvutsv->find('all', array('conditions'=> array('hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cdid)));
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutsvs','chidoan'));
    }
    /**
     * function duyet()
     * Duyet doan vien uu tu, co the duyet theo 1 hoac nhiu doan vien
     * cco 2 chuc nang: duyet, mo khoa 
     * */
    function duyet(){
        $hk=$this->getHocKyHt();
        //$cd=$this->Session->read('chidoanht_id');
        $cdid=$this->data['Dvutsv']['chidoan_id'];
        $duyetids = array();              
		foreach($this->data['Dvutsv']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}
        $chuaduyetids = array();              
		foreach($this->data['Dvutsv']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
		if (count($duyetids) == 0&&count($chuaduyetids) == 0) {
			$this->Session->setFlash(__('Khong co sinh vien nao duoc chon', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaisvs','action'=>'duyetcd',$cdid));
		}else{
            $this->Dvutsv->updateAll(array('Dvutsv.duyet'=>1),array('Dvutsv.id'=>$duyetids));
            $this->Dvutsv->updateAll(array('Dvutsv.duyet'=>0),array('Dvutsv.id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã Lưu !', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaisvs','action'=>'duyetcd',$cdid));
				
		}    
    }

	function view($id = null) {
	   
		if (!$id) {
			$this->Session->setFlash(__('Invalid dvutsv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dvutsv', $this->Dvutsv->read(null, $id));
	}

	function add() {
	   $data=$this->data;
       if (!empty($data)) {
            //for($j=1;$j<count($data['Dvutsv'])/5;$j++){
                //echo $data['Dvutsv']['sinhvien_id'.$j];
                //echo "soluong: ".count($data['Dvutsv']);
                //$dvutsv[]=array('sinhvien_id'=>$data['Dvutsv']['sinhvien_id'.$j],
//                                'hocky_id'=>$data['Dvutsv']['hocky_id'.$j],
//                                'songuoidongy'=>$data['Dvutsv']['songuoidongy'.$j],
//                                'songuoithamgiabau'=>$data['Dvutsv']['songuoithamgiabau'.$j],
//                                'ghichu'=>$data['Dvutsv']['ghichu'.$j]);
    			//$this->Dvutsv->create();
    			$this->Dvutsv->saveField('sinhvien_id',$data['Dvutsv']['sinhvien_id1']);
   				$this->Dvutsv->id=false;
    		//}
        }
        //$this->redirect(array('action' => 'index'));
		//$sinhviens = $this->Dvutsv->Sinhvien->find('list');
		//$hockies = $this->Dvutsv->Hocky->find('list');
		//$this->set(compact('sinhviens', 'hockies'));
	}
    //edit cho sv
    
    /**
     * function editdvut($id = null) 
     * edit dvut voi tham so dua vao la id cua sv
     * */
	function editdvut($id = null) {
	   $lock="";
	   $hk=$this->getHocKyHt();
       $cd=$this->Session->read('chidoanht_id');
       $sinhvien =$this->Z1dvutsv->find('first', array('conditions'=> array('id'=>$id,'hocky_id'=>$hk['Hocky']['id'],'chidoansv_id'=>$cd)));
	   $this->set(compact('sinhvien'));
       if($sinhvien['Z1dvutsv']['duyet']==0){
            if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dvutsv', true));
			$this->redirect(array('action' => 'viewdvut'));
    		}
    		if (!empty($this->data)) {
                
    			if ($this->Dvutsv->save($this->data)) {
    				$this->Session->setFlash(__('The dvutsv has been saved', true));
    				$this->redirect(array('action' => 'viewdvut'));
    			} else {
    				$this->Session->setFlash(__('The dvutsv could not be saved. Please, try again.', true));
    			}
    		}
    		if (empty($this->data)) {
    			$this->data = $this->Dvutsv->read(null, $id);
    		}
       }else{
            $this->Session->setFlash(__('Đã hết thời gian chỉnh sửa đoàn viên ưu tú', true));
            $this->set('lock',$lock);
       }
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid dvutsv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dvutsv->save($this->data)) {
				$this->Session->setFlash(__('The dvutsv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The dvutsv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dvutsv->read(null, $id);
		}
		$sinhviens = $this->Dvutsv->Sinhvien->find('list',array('fields'=>'mssv'));
		$hockies = $this->Dvutsv->Hocky->find('list');
		$this->set(compact('sinhviens', 'hockies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for dvutsv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dvutsv->delete($id)) {
			$this->Session->setFlash(__('Dvutsv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dvutsv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
    
}
